//
//  UIKit+MKUITheme.m
//  MKUI
//
//  Created by 苏尚进 on 2021/3/21.
//  Copyright © 2021 moon. All rights reserved.
//

#import "UIKit+MKUITheme.h"
#import "NSObject+MKUITheme.h"
#import "NSString+MKUITheme.h"
#import "MKUIThemePicker.h"
#import "MKUIThemeStatePicker.h"

@implementation UIView (MKUITheme)
MKTHEME_define_property(alpha, MKUIThemeCGFloatPicker)
MKTHEME_define_property(backgroundColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(tintColor, MKUIThemeUIColorPicker)
@end

@implementation UILabel (MKUITheme)
MKTHEME_define_property(text, MKUIThemeIdPicker)
MKTHEME_define_property(font, MKUIThemeIdPicker)
MKTHEME_define_property(textColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(highlightedTextColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(shadowColor, MKUIThemeUIColorPicker)
@end

@implementation UIImageView (MKUITheme)
MKTHEME_define_property(image, MKUIThemeUIImagePicker)
@end

@implementation CALayer (MKUITheme)
MKTHEME_define_property(backgroundColor,MKUIThemeCGColorRefPicker)
MKTHEME_define_property(borderWidth,MKUIThemeCGFloatPicker)
MKTHEME_define_property(borderColor,MKUIThemeCGColorRefPicker)
MKTHEME_define_property(shadowColor,MKUIThemeCGColorRefPicker)
MKTHEME_define_property(strokeColor,MKUIThemeCGColorRefPicker)
@end

@implementation UIBarButtonItem (MKUITheme)
MKTHEME_define_property(tintColor, MKUIThemeUIColorPicker)
@end

@implementation UINavigationBar (MKUITheme_Color)
- (void)setMk_titleTextColor:(UIColor *)titleTextColor{
    NSMutableDictionary *attr = [[NSMutableDictionary alloc] init];
    [attr addEntriesFromDictionary:self.titleTextAttributes];
    attr[NSForegroundColorAttributeName] = titleTextColor;
    self.titleTextAttributes = attr;
}
- (UIColor *)mk_titleTextColor{
    NSDictionary *attr = self.titleTextAttributes;
    UIColor *color = attr[NSForegroundColorAttributeName];
    return color;
}
- (void)setMk_titleTextFont:(UIFont *)titleTextFont{
    NSMutableDictionary *attr = [[NSMutableDictionary alloc] init];
    [attr addEntriesFromDictionary:self.titleTextAttributes];
    attr[NSFontAttributeName] = titleTextFont;
    self.titleTextAttributes = attr;
}
- (UIFont *)mk_titleTextFont{
    NSDictionary *attr = self.titleTextAttributes;
    UIFont *font = attr[NSFontAttributeName];
    return font;
}
- (void)mk_setBackgroundColorOfBar:(UIColor *)backgroundColor{
    CGRect rect = CGRectZero;
    UIColor *color = backgroundColor;
    rect.size = CGSizeMake(1, 1);
    CGFloat scale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, color.CGColor);
    CGContextFillRect(ctx, rect);
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(imageRef);
    UIGraphicsEndImageContext();
    [self setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
@end

@implementation UINavigationBar (MKUITheme)
MKTHEME_define_property(barStyle, MKUIThemeNSIntegerPicker)
MKTHEME_define_property(barTintColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(titleTextAttributes, MKUIThemeIdPicker)
MKTHEME_define_property(largeTitleTextAttributes, MKUIThemeIdPicker)

- (id<MKUIThemeElementProtocol>)mktheme_titleTextColor{
    MKUIThemeUIColorPicker *picker = [self mktheme_getPickerForKey:[MKUIThemeUIColorPicker pickerKeyForObjSelector:@selector(setMk_titleTextColor:)]];
    return picker.element;
}
- (void)setMktheme_titleTextColor:(id<MKUIThemeElementProtocol>)value{
    [self mktheme_setPicker:[MKUIThemeUIColorPicker pickerForObjSelector:@selector(setMk_titleTextColor:) element:value]];
}

- (id<MKUIThemeElementProtocol>)mktheme_titleTextFont{
    MKUIThemeIdPicker *picker = [self mktheme_getPickerForKey:[MKUIThemeIdPicker pickerKeyForObjSelector:@selector(setMk_titleTextFont:)]];
    return picker.element;
}
- (void)setMktheme_titleTextFont:(id<MKUIThemeElementProtocol>)value{
    [self mktheme_setPicker:[MKUIThemeIdPicker pickerForObjSelector:@selector(setMk_titleTextFont:) element:value]];
}

- (void)mktheme_setBackgroundColorOfBar:(id<MKUIThemeElementProtocol>)value{
    [self mktheme_setPicker:[MKUIThemeUIColorPicker pickerForObjSelector:@selector(mk_setBackgroundColorOfBar:) element:value]];\
}
@end

@implementation UITabBar (MKUITheme)
MKTHEME_define_property(barStyle, MKUIThemeNSIntegerPicker)
MKTHEME_define_property(barTintColor, MKUIThemeUIColorPicker)
@end

@implementation UITableView (MKUITheme)
MKTHEME_define_property(separatorColor, MKUIThemeUIColorPicker)
@end

@implementation UITextField (MKUITheme)
MKTHEME_define_property(font, MKUIThemeIdPicker)
MKTHEME_define_property(keyboardAppearance, MKUIThemeNSIntegerPicker)
MKTHEME_define_property(textColor, MKUIThemeUIColorPicker)
@end

@implementation UITextView (MKUITheme)
MKTHEME_define_property(font, MKUIThemeIdPicker)
MKTHEME_define_property(keyboardAppearance, MKUIThemeNSIntegerPicker)
MKTHEME_define_property(textColor, MKUIThemeUIColorPicker)
@end

@implementation UISearchBar (MKUITheme)
//#if os(iOS)
MKTHEME_define_property(barStyle, MKUIThemeNSIntegerPicker)
//#endif
MKTHEME_define_property(keyboardAppearance, MKUIThemeNSIntegerPicker)
MKTHEME_define_property(barTintColor, MKUIThemeUIColorPicker)
@end

@implementation UIProgressView (MKUITheme)
MKTHEME_define_property(progressTintColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(trackTintColor, MKUIThemeUIColorPicker)
@end

@implementation UIPageControl (MKUITheme)
MKTHEME_define_property(pageIndicatorTintColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(currentPageIndicatorTintColor, MKUIThemeUIColorPicker)
@end

@implementation UIActivityIndicatorView (MKUITheme)
MKTHEME_define_property(activityIndicatorViewStyle, MKUIThemeNSIntegerPicker)
@end

@implementation UIToolbar (MKUITheme)
MKTHEME_define_property(barStyle, MKUIThemeNSIntegerPicker)
MKTHEME_define_property(barTintColor, MKUIThemeUIColorPicker)
@end

@implementation UISwitch (MKUITheme)
MKTHEME_define_property(onTintColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(thumbTintColor, MKUIThemeUIColorPicker)
@end

@implementation UISlider (MKUITheme)
MKTHEME_define_property(thumbTintColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(minimumTrackTintColor, MKUIThemeUIColorPicker)
MKTHEME_define_property(maximumTrackTintColor, MKUIThemeUIColorPicker)
@end

@implementation UIPopoverPresentationController (MKUITheme)
MKTHEME_define_property(backgroundColor, MKUIThemeUIColorPicker)
@end

@implementation UIButton (MKUITheme)
- (id<MKUIThemeElementProtocol>)mktheme_getImageForState:(UIControlState)state{
    MKUIThemeUIImageStatePicker *picker = [self mktheme_getPickerForKey:[MKUIThemeUIImageStatePicker pickerKeyForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"image"] state:state]];
    return picker.element;
}
- (void)mktheme_setImage:(id<MKUIThemeElementProtocol>)value forState:(UIControlState)state{
    [self mktheme_setPicker:[MKUIThemeUIImageStatePicker pickerForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"image"] element:value state:state]];
}
- (id<MKUIThemeElementProtocol>)mktheme_backgroundImageForState:(UIControlState)state{
    MKUIThemeUIImageStatePicker *picker = [self mktheme_getPickerForKey:[MKUIThemeUIImageStatePicker pickerKeyForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"backgroundImage"] state:state]];
    return picker.element;
}
- (void)mktheme_setBackgroundImage:(id<MKUIThemeElementProtocol>)value forState:(UIControlState)state{
    [self mktheme_setPicker:[MKUIThemeUIImageStatePicker pickerForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"backgroundImage"] element:value state:state]];
}
- (id<MKUIThemeElementProtocol>)mktheme_titleColorForState:(UIControlState)state{
    MKUIThemeUIImageStatePicker *picker = [self mktheme_getPickerForKey:[MKUIThemeUIImageStatePicker pickerKeyForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"titleColor"] state:state]];
    return picker.element;
}
- (void)mktheme_setTitleColor:(id<MKUIThemeElementProtocol>)value forState:(UIControlState)state{
    [self mktheme_setPicker:[MKUIThemeUIColorStatePicker pickerForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"titleColor"] element:value state:state]];
}

- (id<MKUIThemeElementProtocol>)mktheme_titleForState:(UIControlState)state{
    MKUIThemeUIImageStatePicker *picker = [self mktheme_getPickerForKey:[MKUIThemeUIImageStatePicker pickerKeyForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"title"] state:state]];
    return picker.element;
}
- (void)mktheme_setTitle:(id<MKUIThemeElementProtocol>)value forState:(UIControlState)state{
    [self mktheme_setPicker:[MKUIThemeStatePicker pickerForObjSelector:[NSString mktheme_setSelectorForStatedProperty:@"title"] element:value state:state]];
}
@end

@implementation UITableViewCell (MKUITheme)
MKTHEME_define_property(selectionStyle, MKUIThemeNSIntegerPicker)
@end
